package com.tim.test.download;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;
/**
 * jQuery选择器规则，https://www.runoob.com/jquery/jquery-ref-selectors.html：
 * 	.选择class，#选择id，[]选择属性
 * 	p,h2；同时选择p、h2
 * 	div.main；同时满足
 * 	div .comment；子孙
 * 	div>p；子级
 * 	div~h1；兄弟
 */
public class MzdHtmlAnt {

	@Test
	public void download() throws Exception {
		// 目标文件夹
		String dir = "C:/Users/Administrator/Desktop/mzd/";
		// 文件名
		String fileName = "传习录.txt";
		// 解析地址
		String hrefAddr = "http://www.quanxue.cn/CT_RuJia/ChuanXiLuIndex.html";
		// 链接url前缀
		String urlPre = "http://www.quanxue.cn/CT_RuJia/";

		/* step1，解析下载地址*/
		Map<String, String> nameHrefMap = getNameHrefMap(hrefAddr, urlPre);

		/* step2，获取内容*/
		StringBuilder builder = new StringBuilder();
		for(Map.Entry<String, String> entry : nameHrefMap.entrySet()){
			String content = getContent(entry.getValue());
			builder.append(entry.getKey()+"\n")
					.append(content+"\n");
		}

		/* step3，写入到文件*/
		File dirFile = new File(dir);
		if(!dirFile.exists()){
			dirFile.mkdirs();
		}
		File file = new File(fileName);
		if(!file.exists()){
			file.mkdirs();
		}
		Files.write(Paths.get(dir,fileName), builder.toString().getBytes(StandardCharsets.UTF_8));
//			HttpUtil.downloadFile(v, fileName);
	}

	private Map<String, String> getNameHrefMap(String hrefAddr, String urlPre) throws IOException {
		Document document = Jsoup.connect(hrefAddr).get();
		// 初筛
		Elements containers = document.body().select("table.indextable");
		// 选中目标元素
		Elements targetTags = containers.select("a[href]");

		// 获取html链接
		Map<String, String> nameHrefMap = new LinkedHashMap<>(128);
		for(Element targetTag : targetTags){
			String href = targetTag.attr("href");
			if(StringUtils.isBlank(href) || href.trim().startsWith("http")){
				continue;
			}
			// 查找所有子孙级a标签，获取下载链接
			nameHrefMap.put(targetTag.text(), urlPre + href);
		}
		nameHrefMap.forEach((k,v)->{
			System.out.println(k+","+v);
		});
		return nameHrefMap;
	}

	private String getContent(String href) throws Exception {
		StringBuilder builder = new StringBuilder();
		Document document = Jsoup.connect(href).get();
		// 初筛
		Elements containers = document.body().select("div.main");
		// 选中目标元素
		Elements targetTags = containers.select("h2,h3,p");

		// 获取内容
		for (Element targetTag : targetTags){
			String text = getText(targetTag);
			builder.append(text).append("\n");
		}

		return builder.toString();
	}

	private String getText(Element child) {
		return child.text();

	}


}
